Cache Event Handler Examples 您所在的位置:网站首页 apache geode Cache Event Handler Examples

Cache Event Handler Examples

2023-08-13 07:32| 来源: 网络整理| 查看: 265

Apache Geode CHANGELOG Cache Event Handler Examples

Some examples of cache event handlers.

Declaring and Loading an Event Handler with Parameters

This declares an event handler for a region in the cache.xml. The handler is a cache listener designed to communicate changes to a DB2 database. The declaration includes the listener’s parameters, which are the database path, username, and password.

. . . JDBCListener jdbc:db2:SAMPLE gfeadmin admin1

This code listing shows part of the implementation of the JDBCListener declared in the cache.xml. This listener implements the Declarable interface. When an entry is created in the cache, this listener’s afterCreate callback method is triggered to update the database. Here the listener’s properties, provided in the cache.xml, are passed into the Declarable.init method and used to create a database connection.

. . . public class JDBCListener extends CacheListenerAdapter implements Declarable { public void afterCreate(EntryEvent e) { . . . // Initialize the database driver and connection using input parameters Driver driver = (Driver) Class.forName(DRIVER_NAME).newInstance(); Connection connection = DriverManager.getConnection(_url, _username, _password); System.out.println(_connection); . . . } . . . public void init(Properties props) { this._url = props.getProperty("url"); this._username = props.getProperty("username"); this._password = props.getProperty("password"); } } Installing an Event Handler Through the API

This listing defines a cache listener using the RegionFactory method addCacheListener.

Region newReg = cache.createRegionFactory() .addCacheListener(new SimpleCacheListener()) .create(name);

You can create a cache writer similarly, using the RegionFactory method setCacheWriter, like this:

Region newReg = cache.createRegionFactory() .setCacheWriter(new SimpleCacheWriter()) .create(name); Installing Multiple Listeners on a Region

XML:

. . . myCacheListener1 myCacheListener2 myCacheListener3

API:

CacheListener listener1 = new myCacheListener1(); CacheListener listener2 = new myCacheListener2(); CacheListener listener3 = new myCacheListener3(); Region nr = cache.createRegionFactory() .initCacheListeners(new CacheListener[] {listener1, listener2, listener3}) .setScope(Scope.DISTRIBUTED_NO_ACK) .create(name); Installing a Write-Behind Cache Listener //AsyncEventQueue with listener that performs WBCL work MyAsyncListener jdbc:db2:SAMPLE gfeadmin admin1 // Add the AsyncEventQueue to region(s) that use the WBCL


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有